/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.db.obj.vis.ui.actions;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.awt.event.ActionEvent;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.opu.db.obj.vis.domain.ObjectService;
import org.opu.db.obj.vis.domain.dao.impl.StorageFactory;
import org.opu.db.obj.vis.domain.domains.ObjectEntry;
import org.opu.db.obj.vis.domain.wrappers.ObjectWrapper;
import org.opu.db_vdumper.actions.DbConnect;
import org.opu.db_vdumper.actions.db.DbAction;
import org.opu.db_vdumper.beans.ApplicationData;
import org.opu.db_vdumper.services.DomainService;
import org.opu.db_vdumper.services.ServiceFactory;
import org.opu.db_vdumper.util.DbManager;

/**
 *
 * @author yura
 */
public class NewDatabase extends AbstractAction {

    private ObjectsList objectsListAction;
    private ObjectService objectService;
    private ApplicationData applicationData;

    @Override
    public void runAction(ActionEvent e) throws Exception {
//        try {
            DbConnect connector = new DbConnect(applicationData, 
                    Maps.<String, DbAction>newHashMap());

            connector.action(e);

            ServiceFactory serviceFactory = applicationData.getServiceFactory();
            DbManager dbManager = applicationData.getDbManager();
            DomainService domainService = serviceFactory.getDomainService(dbManager);
            
            List<ObjectWrapper> baseMapping = objectService.createBaseMapping(domainService);

            List<ObjectEntry> objectEntrys = show(baseMapping);

            objectService.createObjectsMapping(objectEntrys);
//        } finally {
            objectsListAction.runAction(e);
//        }
    }

    public void setApplicationData(ApplicationData applicationData) {
        this.applicationData = applicationData;
    }

    public void setObjectService(ObjectService objectService) {
        this.objectService = objectService;
    }

    public void setObjectsListAction(ObjectsList objectsListAction) {
        this.objectsListAction = objectsListAction;
    }

    @Override
    public String getName() {
        return "NewDatabase";
    }

    protected List<ObjectEntry> show(List<ObjectWrapper> baseMapping) {
        List<ObjectEntry> list = Lists.newArrayList();
        for (ObjectWrapper objectWrapper : baseMapping) {
            list.add(objectWrapper.getObjectEntry());
        }
        return list;
    }

}
